package com.hulk.rpc.utils;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xuxukang
 */
public class AtomicUtils {

  public static int incrementAndGet(AtomicInteger i) {
    int current;
    int next;
    do {
      current = i.get();
      next = current >= 2147483647 ? 0 : current + 1;
    } while (!i.compareAndSet(current, next));

    return next;
  }

  public static int decrementAndGet(AtomicInteger i) {
    int current;
    int next;
    do {
      current = i.get();
      next = current <= 0 ? 2147483647 : current - 1;
    } while (!i.compareAndSet(current, next));

    return next;
  }

}
